﻿
using System.Collections.Generic;
using System.Data;
using System;
namespace ZHDJ.MobileInterface
{
    public partial class GetInformationList : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            // 业务逻辑返回的响应对象  
            InformationOutParam result = new InformationOutParam();
            InformationInParam param = new InformationInParam();
            string logMsg = "";
            bool isFlag = true;
                 
            try
            { 
                // 2，3暂时未实现
                param = inParam as InformationInParam;
                //if (param.type != "1" && param.type != "2" && param.type != "3" && param.type != "4" && param.type != "6" && param.type != "7")
                //{
                //    result.code = "0";
                //    result.desc = "类别只能为1(通知公告),或2(热点新闻),或3(学习十八大),或4(党员先锋),或6(干部工作),或7(人才天地)";

                //    isFlag = false;
                //}
                if (param.type != "1" && param.type != "4" && param.type != "6" && param.type != "7")
                {
                    result.code = "0";
                    result.desc = "类别只能为1(通知公告),或4(党员先锋),或6(干部工作),或7(人才天地)";

                    isFlag = false;
                }

                if (param.page_no * param.page_count <= 0)
                {
                    result.code = "0";
                    result.desc = "当前页,以及页记录条数必须为大于0的正整数";

                    isFlag = false;
                } 

                if(isFlag)
                {
                    DataSet ds = InterfaceHelper.GetInformationList(param.type, param.page_no, param.page_count); 
                    if (ds != null && ds.Tables.Count == 2 )
                    {
                        Information itemTemp; 
                        result.data.total_pages = CommonUtils.GetTotalPages(Convert.ToInt32(ds.Tables[0].Rows[0][0]), param.page_count);
                        foreach (DataRow dr in ds.Tables[1].Rows)
                        {
                            itemTemp = new Information();
                            itemTemp.image_url = dr["image_url"].ToString();
                            itemTemp.msg_id = dr["msg_id"].ToString();
                            try
                            {
                                itemTemp.publish_time = Convert.ToDateTime(dr["publish_time"]).ToString("yyyy-MM-dd HH:mm");
                                itemTemp.push_time = Convert.ToDateTime(dr["push_time"]).ToString("yyyy-MM-dd HH:mm");
                            }
                            catch
                            {
                            }
                            itemTemp.source = CommonUtils.ChangeBranchName(dr["source"].ToString());
                            itemTemp.title = dr["title"].ToString();
                            itemTemp.isfirst = dr["isfirst"].ToString().ToLower() == "true" ? "1" : "0";
                            itemTemp.category = dr["category"].ToString();

                            if (!string.IsNullOrEmpty(itemTemp.image_url))
                                itemTemp.image_url = CommonUtils.GetFileUrl(itemTemp.image_url);

                            result.data.list.Add(itemTemp);
                        }
                    } 
                }
                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "操作异常，多次出现请联系管理员"; 
                logMsg = ex.Message + ":" + ex.StackTrace;
            }

            CommonUtils.InsertMobileInterfaceLog(SystemCode +"-GetInformationList", SerializeHelper.JsonSerializer<InformationInParam>(param), logMsg); 
            return result as OutParam;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<InformationInParam, InformationOutParam>();
            else
                SafeProcessRequest<InformationInParam, InformationOutParam>();
        }
    }

    #region 请求参数
    public class InformationInParam
    {
        /// <summary>
        /// 必填，1：通知公告，2：热点新闻，3：学习十八大，4：党员先锋， 6：干部工作，7：人才天地
        /// </summary>
        public string type
        { get; set; }

        /// <summary>
        /// 必填 当前页
        /// </summary>
        public int page_no
        { get; set; }

        /// <summary>
        /// 必填，每页记录条数
        /// </summary>
        public int page_count
        { get; set; }
    }
    #endregion

    #region 结果参数
    public class InformationOutParam
    {
        public InformationOutParam()
        {
            code = "1";
            desc = "操作成功";
            data = new InformationList();
        }

        /// <summary>
        /// 返回结果 具体见接口返回对照码 1	成功 0	失败 2	请求非法 加密结果和传入的MD5字符串进行比较，如果相同则执行接口操作，否则返回1001错误响应码
        /// </summary>
        public string code
        { get; set; }

        /// <summary>
        /// 结果描述 成功和失败都需要
        /// </summary>
        public string desc
        { get; set; }

        /// <summary>
        /// 资讯列表
        /// </summary>
        public InformationList data
        { get; set; }
    }

    public class InformationList
    {
        public InformationList()
        {
            total_pages = 0;
            list = new List<Information>();
        }

        public List<Information> list { get; set; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int total_pages
        { get; set; }
    }
     
    /// <summary>
    /// 资讯列表
    /// </summary>
    public class Information
    {
        /// <summary>
        /// 资讯ID
        /// </summary>
        public string msg_id
        { get; set; }

        /// <summary>
        /// 资讯标题
        /// </summary>
        public string title
        { get; set; }

        /// <summary>
        ///	图片
        /// </summary>
        public string image_url
        { get; set; }

        /// <summary>
        /// 发布时间
        /// </summary>
        public string publish_time
        { get; set; }

        /// <summary>
        /// 推送时间 yyyy-MM-dd hh:ss
        /// </summary>
        public string push_time
        { get; set; }

        /// <summary>
        /// 通知公告对应作者，热点新闻、党员先锋对应所属组织，学习十八大对应来源
        /// </summary>
        public string source
        { get; set; }

        /// <summary>
        ///  是否置顶 1　是　0 否
        /// </summary>
        public string isfirst
        { get; set; }

        //1：通知公告，2：热点新闻，3：学习十八大，4：党员先锋， 6：干部工作，7：人才天地
        /// <summary>
        /// 咨询类型 如果是
        /// </summary>
        public string category
        { get; set; } 
    }
    #endregion 
}